Make it easier to map dependencies in a summary
authorAlex Crichton <alex@alexcrichton.com>
Thu, 23 Oct 2014 05:14:23 +0000 (22:14 -0700)
committerAlex Crichton <alex@alexcrichton.com>
Mon, 27 Oct 2014 19:40:23 +0000 (12:40 -0700)
This will hopefully become useful in the upcoming changes to resolve for
lockfiles with the registry.

src/cargo/core/dependency.rs
src/cargo/core/source.rs
src/cargo/core/summary.rs
src/cargo/ops/cargo_generate_lockfile.rs
src/cargo/ops/cargo_package.rs
src/cargo/sources/git/source.rs

index cd0d81939c7345b856239f9d31cc72c20ce6d0d1..b57a3431b9e82f551f0e8dc3e95b815d5112cd25 100644 (file)
@@ -100,6 +100,18 @@ impl Dependency {
         self
     }
 
+    /// Set the source id for this dependency
+    pub fn source_id(mut self, id: SourceId) -> Dependency {
+        self.source_id = id;
+        self
+    }
+
+    /// Set the version requirement for this dependency
+    pub fn version_req(mut self, req: VersionReq) -> Dependency {
+        self.req = req;
+        self
+    }
+
     /// Returns false if the dependency is only used to build the local package.
     pub fn is_transitive(&self) -> bool { self.transitive }
     pub fn is_optional(&self) -> bool { self.optional }
index 9644403ea693526088f08246a27ef6ee5b7e6948..15a9e5ae47837d7ba14a49332a912b43bd1174b4 100644 (file)
@@ -157,12 +157,8 @@ impl SourceId {
     }
 
     pub fn for_git(url: &Url, reference: &str, precise: Option<String>) -> SourceId {
-        let mut id = SourceId::new(GitKind(reference.to_string()), url.clone());
-        if precise.is_some() {
-            id = id.with_precise(precise.unwrap());
-        }
-
-        id
+        SourceId::new(GitKind(reference.to_string()), url.clone())
+                 .with_precise(precise)
     }
 
     pub fn for_registry(url: &Url) -> SourceId {
@@ -209,10 +205,10 @@ impl SourceId {
         self.inner.precise.as_ref().map(|s| s.as_slice())
     }
 
-    pub fn with_precise(&self, v: String) -> SourceId {
+    pub fn with_precise(&self, v: Option<String>) -> SourceId {
         SourceId {
             inner: Arc::new(SourceIdInner {
-                precise: Some(v),
+                precise: v,
                 .. (*self.inner).clone()
             }),
         }
index 04abb7eb8356a4dbcae59928ca4961f72a781a62..4862572d50ae26b9443ad78843dd73c6a5263381 100644 (file)
@@ -1,11 +1,13 @@
 use std::collections::HashMap;
+use std::mem;
 
 use semver::Version;
 use core::{Dependency, PackageId, SourceId};
 
 use util::{CargoResult, human};
 
-/// Subset of a `Manifest`. Contains only the most important informations about a package.
+/// Subset of a `Manifest`. Contains only the most important informations about
+/// a package.
 ///
 /// Summaries are cloned, and should not be mutated after creation
 #[deriving(Show,Clone)]
@@ -89,6 +91,12 @@ impl Summary {
     pub fn get_features(&self) -> &HashMap<String, Vec<String>> {
         &self.features
     }
+
+    pub fn map_dependencies(mut self, f: |Dependency| -> Dependency) -> Summary {
+        let deps = mem::replace(&mut self.dependencies, Vec::new());
+        self.dependencies = deps.into_iter().map(f).collect();
+        self
+    }
 }
 
 impl PartialEq for Summary {
index 24baf734923e9674b4c2f550eb85a6004433ed91..6d518380b96c152a9ed1b4c971df3e864f74648a 100644 (file)
@@ -71,7 +71,7 @@ pub fn update_lockfile(manifest_path: &Path,
                 match opts.precise {
                     Some(precise) => {
                         sources.push(dep.get_source_id().clone()
-                                        .with_precise(precise.to_string()));
+                                        .with_precise(Some(precise.to_string())));
                     }
                     None => {}
                 }
index ace986918c85877b28c15fb3ef619627414a4ee7..d0234e3c45fbfde7c327085cd8b07942f4841c90 100644 (file)
@@ -7,7 +7,7 @@ use flate2::{GzBuilder, BestCompression};
 use flate2::reader::GzDecoder;
 
 use core::source::{Source, SourceId};
-use core::{Package, MultiShell, Summary, Dependency};
+use core::{Package, MultiShell, Dependency};
 use sources::PathSource;
 use util::{CargoResult, human, internal, ChainError, Require};
 use ops;
@@ -102,20 +102,12 @@ fn run_verify(pkg: &Package, shell: &mut MultiShell, tar: &Path)
     // implicitly converted to registry-based dependencies, so we rewrite those
     // dependencies here.
     let registry = try!(SourceId::for_central());
-    let new_deps = pkg.get_dependencies().iter().map(|d| {
-        if !d.get_source_id().is_path() { return d.clone() }
-        Dependency::parse(d.get_name(), d.get_specified_req(), &registry)
-                   .unwrap()
-                   .transitive(d.is_transitive())
-                   .features(d.get_features().to_vec())
-                   .default_features(d.uses_default_features())
-                   .optional(d.is_optional())
-    }).collect::<Vec<_>>();
-    let new_summary = Summary::new(pkg.get_package_id().clone(),
-                                   new_deps,
-                                   pkg.get_summary().get_features().clone());
+    let new_summary = pkg.get_summary().clone().map_dependencies(|d| {
+        if !d.get_source_id().is_path() { return d }
+        d.source_id(registry.clone())
+    });
     let mut new_manifest = pkg.get_manifest().clone();
-    new_manifest.set_summary(new_summary.unwrap());
+    new_manifest.set_summary(new_summary);
     let new_pkg = Package::new(new_manifest,
                                &manifest_path,
                                pkg.get_package_id().get_source_id());
index 8938bac559973d96099b5283945e5bb3f6365cec..c2731fa786d62ef5466f1d3ebd232b29abfb6c16 100644 (file)
@@ -176,7 +176,7 @@ impl<'a, 'b> Source for GitSource<'a, 'b> {
 
         try!(repo.copy_to(actual_rev.clone(), &self.checkout_path));
 
-        let source_id = self.source_id.with_precise(actual_rev.to_string());
+        let source_id = self.source_id.with_precise(Some(actual_rev.to_string()));
         let path_source = PathSource::new(&self.checkout_path, &source_id);
 
         self.path_source = Some(path_source);